package com.ev.apis.controller.asl;

import com.alibaba.fastjson.JSON;
import com.ev.apis.model.DsResultResponse;
import com.ev.asl.constant.AslConstant;
import com.ev.asl.domain.AslBarCodeDO;
import com.ev.asl.domain.AslBarCodeTrayDO;
import com.ev.asl.domain.AslComplaintsDO;
import com.ev.asl.dto.AslBarCodeParam;
import com.ev.asl.dto.AslBarCodeTrayParam;
import com.ev.asl.service.AslBarCodeService;
import com.ev.asl.service.AslComplaintService;
import com.ev.framework.annotation.EvApiByToken;
import com.ev.framework.utils.R;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Api(value = "/", tags = "客诉信息API")
@RestController
public class AslComplaintApiController {

    @Autowired
    private AslComplaintService aslComplaintService;


    /**
     * 获取客诉信息
     * @return
     */
    @ApiOperation("获取客诉信息")
    @EvApiByToken(value = "/apis/complaint/list", method = RequestMethod.GET, apiTitle = "获取条码管理列表信息")
    public R list(@ApiParam(value = "当前第几页", required = true) @RequestParam(value = "pageno") Integer pageno,
                  @ApiParam(value = "一页多少条", required = true) @RequestParam(value = "pagesize") Integer pagesize,
                  @ApiParam(value = "产品名称", required = true) @RequestParam(value = "productName",required = false) String productName,
                  @ApiParam(value = "QA接收日期", required = true) @RequestParam(value = "receiveDate",required = false) Date startDate,
                  @ApiParam(value = "QA接收日期", required = true) @RequestParam(value = "receiveDate",required = false) Date endDate) {
        AslComplaintsDO aslComplaintsDO1 = new AslComplaintsDO();

        if (!ObjectUtils.isEmpty(pageno) && !ObjectUtils.isEmpty(pagesize)){
            aslComplaintsDO1.setOffer((pageno-1)*pagesize);
        }
        aslComplaintsDO1.setLimit(pagesize);
        aslComplaintsDO1.setProductName(productName);
        aslComplaintsDO1.setStartDate(startDate);
        aslComplaintsDO1.setEndDate(endDate);
        Integer total = aslComplaintService.count(aslComplaintsDO1);
        Map<String,Object> result = Maps.newHashMap();
        if (total > 0){
            List<AslComplaintsDO> data = aslComplaintService.list(aslComplaintsDO1);
            result.put("data", new DsResultResponse(pageno,pagesize,total,data));
        }
        return R.ok(result);
    }


    /**
     * 查找客诉信息
     * @param id
     * @return
     */
    @ApiOperation("查找客诉信息")
    @EvApiByToken(value = "/apis/complaint/getById",
            method = RequestMethod.GET,
            apiTitle = "查找客诉信息")
    public R getById(@ApiParam(value = "查找客诉信息", required = true) @RequestParam(value = "id", defaultValue = "") Long id) {
        AslComplaintsDO aslComplaintsDO = aslComplaintService.get(id);
        Map<String,Object> result = Maps.newHashMap();
        result.put("data",aslComplaintsDO);
        return R.ok(result);
    }

    /**
     * 新增客诉信息
     * @return
     */
    @ApiOperation("新增客诉信息")
    @EvApiByToken(value = "/apis/complaint/save",
            method = RequestMethod.POST,
            apiTitle = "新增客诉信息")
    public R save(@ApiParam(value = "客诉信息", required = true)@RequestBody AslComplaintsDO aslComplaintsDO) {
        Map<String, Object> result = Maps.newHashMap();
        int save = aslComplaintService.save(aslComplaintsDO);
        return save > 0 ? R.ok(result) : R.error();
    }

    /**
     * 修改客诉信息
     * @return
     */
    @ApiOperation("修改客诉信息")
    @EvApiByToken(value = "/apis/complaint/update",
            method = RequestMethod.POST,
            apiTitle = "修改客诉信息")
    public R update(@ApiParam(value = "客诉信息", required = true)@RequestBody AslComplaintsDO aslComplaintsDO) {
        int save = aslComplaintService.update(aslComplaintsDO);
        return save > 0 ? R.ok() : R.error();
    }


    /**
     * 删除客诉信息
     * @return
     */
    @ApiOperation("删除客诉信息")
    @EvApiByToken(value = "/apis/complaint/delete", method = RequestMethod.GET, apiTitle = "删除客诉信息")
    public R delete(@ApiParam(value = "主键ID", required = true) @RequestParam(value = "ids", defaultValue = "") List<Long> ids) {
        Integer integer = aslComplaintService.batchDeleteTray(ids);
        return integer > 0 ? R.ok() : R.error();
    }
}
